<?php
namespace Kuoqi\Api\Weixin;

/**
 * Class Config
 * @package Kuoqi\Api\Weixin
 * User kuoqi
 * Datetime 2022/3/2 16:33
 * Remark 基础配置信息
 */
class Config{

    //微信小程序appid
    static public $applet_appid = '';//wxe9723f6169e5f50a

    //微信小小程序AppSecret
    static public $applet_secret = '';//
    static public $mp_appid = '';//
    static public $mp_secret = '';//

    //关联的微信商户平台
    static public $mch_id = '';//
    static public $mch_key = '';//

    /**
     * 获取access_token的信息
     * @var string
     */
    static protected $access_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
    static protected $ticket = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi';

    static public function __initialize(){
        self::$applet_appid = \Kuoqi\Config::get('WX_APPLET_ID');
        self::$applet_secret = \Kuoqi\Config::get('WX_APPLET_SECRET');
        self::$mp_appid = \Kuoqi\Config::get('WX_MP_ID');
        self::$mp_secret = \Kuoqi\Config::get('WX_MP_SECRET');
        self::$mch_id = \Kuoqi\Config::get('WX_MCH_ID');
        self::$mch_key = \Kuoqi\Config::get('WX_MCH_KEY');
    }

    /**
     * 初始化accessToken
     * vbase
     * @return mixed
     */
    static public function initAccessToken(){
        $url = sprintf(self::$access_token,
            self::$mp_appid,
            self::$mp_secret
        );
        $response = new Mp\Response(tools::sendTo($url));
        $res = $response->toArray();
        if($res['errcode']==0){
            //这个是正常的情况下，会保存正常的access_toke
            Tools::setCache('access_token_'.self::$mp_appid,$res['data']['access_token'],$res['data']['expires_in'] - 60);
            return $res['data']['access_token'];
        }else{
            return '';
        }
    }

    /**
     * 检测accessToken的相关信息
     * @return array
     */
    static public function checkAccessToken(){
        $url = sprintf(self::$access_token,
            self::$mp_appid,
            self::$mp_secret
        );
        $response = new Mp\Response(tools::sendTo($url));
        $res = $response->toArray();
        if($res['errcode']==0){
            //这个是正常的情况下，会保存正常的access_toke
            Tools::setCache('access_token_'.self::$mp_appid,$res['data']['access_token'],$res['data']['expires_in'] - 60);
        }
        return $res;
    }

    /**
     * 获取 accessToken
     * @return mixed|null
     */
    static public function getAccessToken(){
        if($token = Tools::getCache('access_token_'.self::$mp_appid)){
            return $token;
        }else{
            return self::initAccessToken();
        }
    }

    static public function initTicket(){
        $url = sprintf(self::$ticket,
            self::getAccessToken()
        );
        $response = new Mp\Response(tools::sendTo($url));
        $res = $response->toArray();
        if($res['errcode']==0){
            //这个是正常的情况下，会保存正常的access_toke
            Tools::setCache('ticket_'.self::$mp_appid,$res['data']['ticket'],$res['data']['expires_in'] - 60);
            return $res['data']['ticket'];
        }else{
            return '';
        }
    }

    //设置ticket
    static public function getTicket(){
        if($token = Tools::getCache('ticket_'.self::$mp_appid)){
            return $token;
        }else{
            return self::initTicket();
        }
    }
}